package com.lwj.liwanjia.view.activity.indent;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.FapiaoBean;
import com.lwj.liwanjia.bean.PriceBean;
import com.lwj.liwanjia.impl.FapiaoView;
import com.lwj.liwanjia.presenter.FapiaoPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FaPiaoActivity extends BaseActivity implements FapiaoView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_fapiaojine)
    TextView tvFapiaojine;
    @BindView(R.id.tv_dianzifapiao)
    TextView tvDianzifapiao;
    @BindView(R.id.tv_zhizifanpiao)
    TextView tvZhizifanpiao;
    @BindView(R.id.tv_shangpin)
    TextView tvShangpin;
    @BindView(R.id.tv_qiye)
    TextView tvQiye;
    @BindView(R.id.tv_zhengfu)
    TextView tvZhengfu;
    @BindView(R.id.tv_geren)
    TextView tvGeren;
    @BindView(R.id.ed_gongsi)
    EditText edGongsi;
    @BindView(R.id.ed_shibiema)
    EditText edShibiema;
    @BindView(R.id.ll_shibiema)
    LinearLayout llShibiema;
    @BindView(R.id.ed_emil)
    EditText edEmil;

    private int type = 1;//发票类型
    private int section = 0;//申请方
    private FapiaoPresenter presenter;
    private String token;
    private String liwanjiaInvoiceId;//发票ID
    private Map<String, Object> map = new HashMap<>();
    private String price;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("填写发票信息");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        price = getIntent().getStringExtra("price");
        tvFapiaojine.setText("开票金额" + price);
        token = SpfUtils.getSpfUtils(this).getToken();
        presenter = new FapiaoPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fa_piao;
    }


    @OnClick({R.id.tv_left, R.id.tv_right, R.id.tv_dianzifapiao, R.id.tv_zhizifanpiao, R.id.tv_qiye, R.id.tv_zhengfu, R.id.tv_geren})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left://返回
                finish();
                break;
            case R.id.tv_right://保存
                getMaps();
                break;
            case R.id.tv_dianzifapiao://电子发票
//                tvDianzifapiao.setTextColor(getResources().getColor(R.color.colorEC921A));
//                tvDianzifapiao.setBackgroundResource(R.drawable.fapiao_shape);
//                tvZhizifanpiao.setTextColor(getResources().getColor(R.color.colorCECECE));
//                tvZhizifanpiao.setBackgroundResource(R.drawable.fapiao_shape1);
//                type = 0;
                break;
            case R.id.tv_zhizifanpiao://纸质发票
                tvDianzifapiao.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvDianzifapiao.setBackgroundResource(R.drawable.fapiao_shape1);
                tvZhizifanpiao.setTextColor(getResources().getColor(R.color.colorEC921A));
                tvZhizifanpiao.setBackgroundResource(R.drawable.fapiao_shape);
                type = 1;
                break;
            case R.id.tv_qiye://企业
                edGongsi.setHint("请输入公司名称");
                tvQiye.setTextColor(getResources().getColor(R.color.colorEC921A));
                tvZhengfu.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvGeren.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvQiye.setBackgroundResource(R.drawable.fapiao_shape);
                tvZhengfu.setBackgroundResource(R.drawable.fapiao_shape1);
                tvGeren.setBackgroundResource(R.drawable.fapiao_shape1);
                llShibiema.setVisibility(View.VISIBLE);
                section = 0;
                break;
            case R.id.tv_zhengfu://政府单位
                edGongsi.setHint("请输入政府单位名称");
                tvQiye.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvZhengfu.setTextColor(getResources().getColor(R.color.colorEC921A));
                tvGeren.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvQiye.setBackgroundResource(R.drawable.fapiao_shape1);
                tvZhengfu.setBackgroundResource(R.drawable.fapiao_shape);
                tvGeren.setBackgroundResource(R.drawable.fapiao_shape1);
                llShibiema.setVisibility(View.GONE);
                section = 1;
                break;
            case R.id.tv_geren://个人
                edGongsi.setHint("请输入名字");
                tvQiye.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvZhengfu.setTextColor(getResources().getColor(R.color.colorCECECE));
                tvGeren.setTextColor(getResources().getColor(R.color.colorEC921A));
                tvQiye.setBackgroundResource(R.drawable.fapiao_shape1);
                tvZhengfu.setBackgroundResource(R.drawable.fapiao_shape1);
                tvGeren.setBackgroundResource(R.drawable.fapiao_shape);
                llShibiema.setVisibility(View.GONE);
                section = 2;
                break;
        }
    }

    private void getMaps() {
        map.put("token", token);
        map.put("invoiceMoney", tvFapiaojine.getText().toString());
        map.put("invoiceContent", "商品");
        if (section == 0) {
            map.put("invoiceProposer", "企业");
        } else if (section == 1) {
            map.put("invoiceProposer", "政府单位");
        } else if (section == 2) {
            map.put("invoiceProposer", "个人");
        }
        String name = edGongsi.getText().toString();
        String emil = edEmil.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "发票抬头必须填写", Toast.LENGTH_SHORT).show();
            return;
        }
        map.put("invoiceTitle", name);
        map.put("invoiceMail", emil);
        String s = edShibiema.getText().toString();
        if (!TextUtils.isEmpty(s) && s != null) {
            map.put("invoiceTaxpayerCode", s);
        }
        presenter.saveFapiaoData(map);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "填写发票失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void saveFapiaoData(FapiaoBean fapiaoBean) {
        if (fapiaoBean.getState() == 0) {
            Toast.makeText(this, "填写发票失败", Toast.LENGTH_SHORT).show();
        } else {
            liwanjiaInvoiceId = fapiaoBean.getData().getInvoiceId();
            Intent intent = new Intent();
            intent.putExtra("type", type);
            intent.putExtra("liwanjiaInvoiceId", liwanjiaInvoiceId);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
